package xcmg.syn.infra;

import java.util.GregorianCalendar;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import yb.ecp.fast.infra.infra.log.LogHelper;

/**
 * @author LCJ
 * @create 2018-06-11 19:22
 **/
public class DateUtil
{
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 时间转换<br>
     *     将时间格式转换成字符串，yyyy-MM-dd
     * @param date Date
     * @return String
     */
    public static String formatDate(Date date)
    {
        if(null==date)
        {
            return "";
        }
        return DateFormatUtils.format(date, "yyyy-MM-dd");
    }

    /**
     * 时间转换<br>
     *     将字符串 转换成 时间格式转换成，yyyy-MM-dd
     * @param date Date
     * @return String
     */
    public static Date stringFormatDate(String date)
    {
        if(StringUtils.isBlank(date))
        {
            return null;
        }
        try {
            return simpleDateFormat.parse(date);
        } catch (ParseException e) {
            LogHelper.fatal("时间格式转换异常", e);
        }
        return null;
    }

    /**
     * 时间转换<br>
     *     将时间格式转换成字符串，yyyy-MM-dd HH:mm:ss
     * @param date Date
     * @return String
     */
    public static String formatDateTime(Date date)
    {
        if(null==date)
        {
            return "";
        }
        return DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date getStartTime(int type){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, -type);
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        try {
            return sdf.parse(DateUtil.formatDate(calendar1.getTime())+" 00:00:00");
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date getEndTime(int type){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, -(type-1));
        calendar1.set(Calendar.DAY_OF_MONTH, 0);
        try {
            return sdf.parse(DateUtil.formatDate(calendar1.getTime())+" 23:59:59");
        } catch (ParseException e) {
            return null;
        }
    }

    public static String getMonth(int type){
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMM");
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, -type);
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        return sdf2.format(calendar1.getTime());
    }


    /**
     * 获取指定时间所在月第一天的0时0分0秒
     * @param date
     * @return
     */
    public static Date getMonthStartTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 0);
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        Date result = null;
        try {
            result = sdf.parse(DateFormatUtils.format(c.getTime(), "yyyy-MM-dd") + " 00:00:00");
        } catch (ParseException e) {
            LogHelper.fatal("DateUtil#getMonthStartTime is error.", e);
        }
        return result;
    }

    /**
     * 获取指定时间所在月最后一天的23:59:59
     * @param date
     * @return
     */
    public static Date getMonthEndTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        //设置为月最后一天
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date result = null;
        try {
            result = sdf.parse(DateFormatUtils.format(c.getTime(), "yyyy-MM-dd") + " 23:59:59");
        } catch (ParseException e) {
            LogHelper.fatal("DateUtil#getMonthEndTime is error.", e);
        }
        return result;
    }


    /**
     * 获取date后的x月的时间，如果x为负数，则是之前x月
     * @param date
     * @param n 月数
     * @return
     */
    public static Date getAfterMonthDate(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, n);
        return calendar.getTime();
    }

    /**
     * 获取date日期当天的开始时间
     */
    public static Date getStartTimeOfDay(Date date) {
        if (null == date) {
            date = new Date();
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取date日期当天的结束时间
     */
    public static Date getEndTimeOfDay(Date date) {
        if (null == date) {
            date = new Date();
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 计算date后的days天的日期
     *
     * @param date
     * @param days
     * @return
     */
    public static Date calNewDate(Date date, int days) {
        if (null == date) {
            return date;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, days);
        return cal.getTime();
    }

    /**
     * date增加指定的分钟数。
     *
     * @param date
     * @param minutes 加的分钟数量。正数为加，负数为减
     * @return
     */
    public static Date getMinuteAddTime(Date date, int minutes) {
        if (null == date) {
            date = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minutes);
        return calendar.getTime();
    }

    /**
     * 加上指定长度的月份后的时间，负数为减
     *
     * @param date
     * @param monthSize
     * @return
     */
    public static Date addMonth(Date date, int monthSize) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.MONTH, monthSize);
        return cl.getTime();
    }
}
